package com.lagou.es.service.impl;

import com.lagou.es.service.PositionService;
import com.lagou.es.util.DBHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;


@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private RestHighLevelClient client;

    private static final String POSITION_INDEX = "position";

    @Override
    public List<Map<String, Object>> searchPos(String keyword, int pageNo, int pageSize) throws IOException {
        if (pageNo <= 1) {
            pageNo = 1;
        }

        // 搜索
        SearchRequest searchRequest = new SearchRequest(POSITION_INDEX);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 分页设置
        searchSourceBuilder.from((pageNo - 1) * pageSize);
        searchSourceBuilder.size(pageSize);


        QueryBuilder builder = QueryBuilders.termQuery("positionName", keyword);
//        QueryBuilder builder = QueryBuilders.termQuery("positionAdvantage", keyword);
//        QueryBuilder builder = QueryBuilders.termQuery("positionAdvantage", "美女多");
        searchSourceBuilder.query(builder);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        // 执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        ArrayList<Map<String, Object>> list = new ArrayList<>();
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            list.add(hit.getSourceAsMap());
        }

        if ((hits.length < 5)) {

            SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
            // 分页设置
            searchSourceBuilder.from((pageNo - 1) * pageSize);
            searchSourceBuilder.size(pageSize);

//            QueryBuilder builder2 = QueryBuilders.termsQuery("positionAdvantage", "美女多","员工福利好");
            QueryBuilder builder2 = QueryBuilders.termsQuery("positionAdvantage", "美女","福利");
            searchSourceBuilder2.query(builder2);
            searchSourceBuilder.fetchSource(new String[]{"positionAdvantage"},new String[]{});
            searchSourceBuilder2.timeout(new TimeValue(60, TimeUnit.SECONDS));
            // 执行搜索
            searchRequest.source(searchSourceBuilder2);
            SearchResponse searchResponse2 = client.search(searchRequest, RequestOptions.DEFAULT);

            SearchHit[] hits2 = searchResponse2.getHits().getHits();
            int size = list.size();
            for (int i = 0; i < 5 - size; i++) {
                if (hits2[i] != null) {
                    list.add(hits2[i].getSourceAsMap());
                }
            }

        }

        return list;
    }


}

